<template>
    <ListPage ref="page" :query="query" :api="getCustomerList">

        <template #search="{ page, hiddenColumns }">
            <PageSearch ref="search" :query="query" :page="page" has-box @search="() => ($refs.page as any).getData()">
                <template #auth>
                    <ElButton type="success" @click="openZdycx">自定义查询</ElButton>
                    <ElButton v-if="auth.has('add')" type="primary" @click="onAddCustomer">添加新客户</ElButton>
                </template>
                <template #input="{ }">
                    <SearchInput v-model="query.keyword" placeholder="客户名称/店铺名称" :data="query"
                        :hidden-columns="(hiddenColumns as any)" @trigger="(e) => ($refs.search as any).trigger(e)"
                        @search="() => ($refs.page as any).getData()"
                        @change-column="(prop) => ($refs.page as any).changeColumn(prop)"></SearchInput>
                </template>
            </PageSearch>
        </template>

        <template #table="{ data, hiddenColumns }">
            <ListTable :data="data" :hidden-columns="(hiddenColumns as any)" @edit-custom="onEditCustom"
                @open-fp="openFp">
            </ListTable>
        </template>
    </ListPage>

    <CustomizeQuery :zdycxObj="zdycxObj" moduleName="咨询记录" @query-data="queryData"></CustomizeQuery>

</template>

<script setup lang="ts">

import { ref, reactive } from 'vue'
import ListPage from '@/components/list-page/ListPage.vue'
import { getCustomerList } from '../api'
import ListTable from './ListTable.vue'
import PageSearch from '@/components/list-page/PageSearch.vue'
import SearchInput from '@/components/list-page/SearchInput.vue'
import { useAuth } from '@/utils/auth'
import { addDialog, editDialog, fpDialog } from '../dialog/customerDialog'
import CustomizeQuery from '@/views/menus/n-ykf-customize-query/components/index.vue'

const page = ref()

const query = reactive({
    keyword: '',
    customerName: '',
    customizeQueryCondition: []
})

const zdycxObj = reactive({
    isShow: false
});

const auth = useAuth()

const onAddCustomer = async () => {
    await addDialog.open();
    // 刷新数据
    page.value && page.value.getData();
}

const onEditCustom = async (row) => {
    row.once = true;
    await editDialog.open({ row });
    // 刷新数据
    page.value && page.value.getData();
}

// 打开自定义查询
const openZdycx = async (row) => {
    zdycxObj.isShow = true;
}

const openFp = async (row) => {
    await fpDialog.open({ row });
}

const onShowDetails = async (row) => {
    alert("查看详情页");
}

const queryData = async (customizeQueryCondition) => {
    zdycxObj.isShow = false;
    query.customizeQueryCondition = customizeQueryCondition;
    page.value?.getData()
}

</script>